package com.ln.controller.impl;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.WebUtils;

import com.ln.common._ObjFac;
import com.ln.controller.BaseController;
import com.ln.pojo.impl.NotesCollectPOJO;
import com.ln.pojo.impl.UserPOJO;
import com.ln.service.impl.NotesCollectService;
/**
 * 收藏笔记
 */
@Controller
public class NotesCollectController extends BaseController {
	@Autowired
	private NotesCollectService notesCollectService;
	
	@RequestMapping(value = "/addCollect", method = RequestMethod.POST)
	@ResponseBody
	public Object addCollect(HttpServletRequest request, NotesCollectPOJO notesCollectPOJO) {
		
		
		UserPOJO LOGIN_USER = (UserPOJO)WebUtils.getSessionAttribute(request, "LOGIN_USER");
		if (LOGIN_USER == null) {//用户未登录
			return _ObjFac.cloneResponseBodyJSON(-1,"您没有登录！", null);
		}
		
		if (LOGIN_USER.getId() == notesCollectPOJO.getNotes_userid()) {
			return _ObjFac.cloneResponseBodyJSON(-5, "不能收藏自己的内容哦！", null);
		}
		notesCollectPOJO.setUser_id(LOGIN_USER.getId());
		notesCollectService.insert(notesCollectPOJO);
		
		return _ObjFac.cloneResponseBodyJSON(1,"取消收藏","/delCollect");
	}

	@ResponseBody
	@RequestMapping(value = "/delCollect", method = RequestMethod.POST)
	public Object delCollect(HttpServletRequest request, NotesCollectPOJO notesCollectPOJO) {
		
		UserPOJO LOGIN_USER = (UserPOJO)WebUtils.getSessionAttribute(request, "LOGIN_USER");
		if (LOGIN_USER == null) {//用户未登录
			return _ObjFac.cloneResponseBodyJSON(-1,"您没有登录！", null);
		}
		
		if (LOGIN_USER.getId() == notesCollectPOJO.getNotes_userid()) {
			return _ObjFac.cloneResponseBodyJSON(-5, "不能删除别人的内容哦！", null);
		}
		
		notesCollectPOJO.setUser_id(LOGIN_USER.getId());
		notesCollectService.deleteByObj(notesCollectPOJO);
		
		return _ObjFac.cloneResponseBodyJSON(1,"收藏","/addCollect");
	}
	
}